"use strict";

const Controller = require("egg").Controller;

class HousingController extends Controller {
  // 获取房源管理列表
  async index() {
    const { ctx, app } = this;
    let { value = "" } = ctx.request.body;
    const data = await app.mysql.query(
      `select * from controls where name like '%${value}%'`
    );
    ctx.body = {
      code: 200,
      data,
    };
  }

  //  房源管理删除
  async delete() {
    const { ctx, app } = this;
    const { id } = ctx.request.body;
    await app.mysql.delete("controls", { id });
    ctx.body = {
      code: 200,
      mess: "删除成功",
    };
  }

  //添加
  async housingadd() {
    const { ctx, app } = this;
    const { name, district, tel, developers, total_prices, packet, architecture, area, 	plotratio, 	planningnumber, property, propertyfee, 	periodint, averag, 	averageprice, panoramathechecking, 	realestateisintroduced, areaofstructure, 	afforest, stall, 	propertyanagementcompany,	forwardsale, time, img } = ctx.request.body;
    const data = await app.mysql.insert("controls", {
      name, district, time, img, tel, total_prices, developers, packet, architecture, area, 	plotratio, 	planningnumber, property, propertyfee, 	periodint, averag, averageprice, panoramathechecking, 	realestateisintroduced, areaofstructure, 	afforest, stall, 	propertyanagementcompany, forwardsale
    });
    if (data.affectedRows) {
      ctx.body = {
        code: 1,
        mes: "添加成功",
        data,
      };
    } else {
      ctx.body = {
        code: 0,
        msg: "添加失败",
      };
    }
  }
}

module.exports = HousingController;
